clear	
set more off
if c(username)=="Scognamillo"	{
gl drive		"C:\Users\Scognamillo\Dropbox\FAO\Epic\Malawi\SPIA adoption\Antonio Scognamillo/IHPS"
gl tmp_panel	"$drive/tmp/josh"								
gl slm_do 		"$drive/panel_creation"
gl maps			"$drive/maps"
gl graphs 		"$drive/graphs"
gl out			"$drive/out"
gl tables		"$drive/tables/raw"
								}
								
else if c(username)=="Antonio"	{
gl drive		"C:\Users\Antonio\Dropbox\FAO\EPIC\malawi\SPIA adoption\Antonio Scognamillo/IHPS"
gl tmp_panel	"$drive/tmp/josh"								
gl slm_do 		"$drive/panel_creation"
gl maps			"$drive/maps"
gl graphs 		"$drive/graphs"
gl out			"$drive/out"
gl tables		"$drive/tables/raw"

}					 			





/*

*-> The code commented out here makes the dataset used in the analysis that follows. 
gl name_for_panel	SLMpanel_27May2016
*do "$slm_do/create_panel_SLMversion.do" 

********************************************************************************
********************************************************************************
********************************************************************************
********CREATE SLM (sustainable land managment) VARIABLES by JOSH BRUBAKER******
********************************************************************************
********************************************************************************
********************************************************************************

********************************************************************************
	**	this dataset was created by MWpanel_SLMuptake_v13.do, and the do-files called therein. 
use "$tmp_panel/SLMpanel_27May2016.dta", clear 
********************************************************************************
bys time : distinct case_id y2_hhid if keepcases==1

keep if keepcases==1	//	balances the panel
tab time
svyset psu [pw=hh_wgt], strata(stratum) 
svydescribe hh_wgt

	*	temperature options
des tmax_00_rs_*
summarize tmax_00_rs_*
des rain_*
summarize rain_*
des fisp_*, f	//	this is the new set of FISP stuff
summarize fisp_*, sep(5)

	**	X variables 	//	updates
loc crop	crop_veg 
loc plot 	soil_sand soil_clay dambo lnha
loc dcsn	dcsnmkr_lnage dcsnmkr_fem
loc hh		adulteq deprat hhmax_ed_years p_offarm  
loc head	hhead_lnage hhead_fem
loc ethrel	tumbuka other_eth muslim other_rel	//	modal chewa & christian are excluded
loc wlth	hhwlth agwlth 
loc com		com_assets lnaccess_index LandDif_1090_GPS roaddensity_pri
loc new		c_agextofficer
loc admin 	fisp_fertbagspc aedosper00hh total_slm distpoorpct
loc clmt	rain_rs_mean rain_rs_cov tmax_00_rs_hi1avg  
loc cond	ea_slope /*ea_wetnessindex ageco2 ageco3 ageco4*/ favorable_soil
pwcorr `clmt' `cond' `ageco' ageco*
loc base	northern central	southern
gl x_plot23	`crop' `plot' `dcsn' `hh' `ethrel' `wlth' `com' `new' `admin' `clmt' `cond' `base'
gl x_hh23	`hh' `head' `ethrel' `wlth' lnlandholdimp `com' `new' `admin' `clmt' `cond' `base'
gl x_ea23	`com' `new' `admin' `clmt' `cond' `base' 
gl order23	time `crop' `plot' `dcsn' `hh' `head' `ethrel' `wlth' lnlandholdimp `com' `new' `admin' `clmt' `cond' `base'
pwcorr $order23
 *-> some of the plot and hh vars are highly correlated but we don't care since the
   * offenders won't be used together at the same time. (ex fem plot dcsnmkr & fem head)
 *-> hhmax_ed_years = .434 with hhwlth... .236 with agwlth
 *-> hhwlth & agwlth = .3193
 *-> agwlth & lnlandholdimp = .3467
 *-> tumbuka > .3 for rain cov & ageco4, .7 with northern
 *-> other eth = -.42 with central
 *-> lnlandholdimp & LandDif = .2997
 *-> com_assets & c_agextofficer = .3166
 *-> total_slm & lnaccessindex = .3894 ... smirk, not surprising
 *-. lnaccesindex & ageco4 = -.4507, lnaccessindex & northern = -.5904 
 *-> roaddensity_pri & aedosper00hh = .4273
 *-> tmax_00_rs_hi1avg & fisp_fertbagspc = -.3304
 *-> total_slm & aedosper00hh = -.3054
 *-> rain_rs_cof & total_slm = -.4160
 *-> rain_rs_mean & rain_rs_cov = -.3115, tmax_00_rs_hi1mean & rain_rs_mean = -.3751
 *-> rain_rs_cov & lnaccessindex = -.4979
des $order23 , f

	**	Cross-Section Analysis Vars 
gl x_sect23 neighbor_* neighbin_* neigh_ea_* 
des $x_sect23
********************************************************************************
	**	Y vars - Binaries 
********************************************************************************
tab tillage_type year, m
la li tillage_type
svy : tab tillage_type year, stubw(32)
egen zerotill = anymatch(tillage_type), v(4)
egen mintill = anymatch(tillage_type), v(3 4 5 6)
egen anyslmtill = anymatch(tillage_type), v(2 3 4 5 6)

recode zerotill mintill anyslmtill (0=.) if time==0
la var zerotill			"Plot was prepared with zero-till methods"
la var mintill			"MSD"  /*"Plot was prepared with minimum soil disturbance methods"*/
la var anyslmtill		"Plot was prepared with any SLM tillage method"
ta tillage_start
la li tillage_start	// no way to do this at the plot level... 

ta tillage_tool1 year, m	//	not much useful here 


tab erosion_ctrl1 year, m
tab erosion_ctrl2 year, m
lab li erosion_ctrl
egen terrace = anymatch(erosion_ctrl1 erosion_ctrl2), v(2)
egen bund_ec = anymatch(erosion_ctrl1 erosion_ctrl2), v(3)
egen bund_wh = anymatch(erosion_ctrl1 erosion_ctrl2), v(7)
egen gabion = anymatch(erosion_ctrl1 erosion_ctrl2), v(4)
egen vetiver = anymatch(erosion_ctrl1 erosion_ctrl2), v(5)
egen treebelt = anymatch(erosion_ctrl1 erosion_ctrl2), v(6)
egen drainage = anymatch(erosion_ctrl1 erosion_ctrl2), v(8)
egen swc_any = anymatch(erosion_ctrl1 erosion_ctrl2), v(2 3 4 5 6 7 8)
egen swc_mech = anymatch(erosion_ctrl1 erosion_ctrl2), v(2 3 4 7 8)
egen swc_bio = anymatch(erosion_ctrl1 erosion_ctrl2), v(5 6)
la var terrace			"Plot has terraces to control erosion"
la var bund_ec			"Plot has bunds to control erosion"
la var bund_wh			"Plot has bunds to harvest water"
la var gabion			"Plot has gabions/sandbags to control erosion"
la var vetiver			"Plot has vetiver grass to control erosion"
la var treebelt			"Plot has tree belts to control erosion"
la var drainage			"Plot has drainage ditches to control erosion"
la var swc_any			"Any erosion control structure"
la var swc_mech			"Mechanical erosion control structure" /*"Plot has any mechanical erosion control structure"*/
la var swc_bio			"Biological erosion control structure" /*"Plot has any biological erosion control structure"*/


tab irrigation_system1 year, m
tab irrigation_method year, m
tab irrigation_source year, m
la li irrigation_system
egen irrigation_any = anymatch(irrigation_system1 irrigation_system2), v(1 2 3 4 5 6)
la var irrigation_any	"Plot has any irrigaton system"


bys year : su fert_*
recode fert_org_use fert_inorg_use (.=0)
la var fert_org_use		"Organic fertilizer" /*"Plot was fertilized with organic fertilizer"  */
la var fert_inorg_use	"Plot was fertilized with inorganic fertilizer"


tab residue_use year, m 
la li residue_use
egen mulch = anymatch(residue_use), v(4 5) 
egen burn = anymatch(residue_use), v(2 3)
recode mulch burn (0=.) if time==0
la var mulch  "Residue retention"     /*"Plot residue was cut and spread over plot last year" */
la var burn	  "Crop residue burned" /* "Plot residue was burnt on plot last year"  */


tab covercrop_any year, m
recode covercrop_any (.=0) if time==1
recode covercrop_any (0=.) if time==0
la var covercrop_any	"Cover crop" /*"Plot was planted with any cover crop last year" */

bys year : ta intleg_1 intleg_2, m
recode intleg_* (.=0)
la var intleg_1			"Legumes intercropping"  /*"Plot was planted with a legume & another crop"*/
la var intleg_2			"Plot was entirely planted with a legume & another crop"


********** IN ORDER TO MODIFY THE PANEL CREATED BY JOSH IS NECESSARY OPEN THE SINGLE WAVE (WE HAVE 2 WAVES IHS3 and IHPS) IN "PANEL CREATION" 
***********THEN SAVE A NEW FILE WITH THE NEW VARIABLE(s) AND THEN MERGE  THIS NEW FILE FOLLOWING THE PROCEDURE BELOW
********** (FOR THE VARIABLE BELOW I HAVE MODIFIED THE FILES RELATIVE TO THE MODULE G IN THE "PANEL CREATION" FOLDER)
tempfile ihps_intercrop ihs3_intercrop intercrop

preserve
drop if time==0
merge 1:1 y2_hhid plotid using "$tmp_panel/intercrop_ihps",  nogenerate 
save `ihps_intercrop'
restore
drop if time==1
merge 1:1  case_id plotid using "$tmp_panel/intercrop_ihs3" ,  nogenerate 
save `ihs3_intercrop'
append using `ihps_intercrop'

bys time: ta intercrop_other
recode intercrop_other (.=0)
la var intercrop_other "Intercropping (no-legumes)"  /*"Plot was planted with more than one crop" */
***************************************************************************************************************************
**************************************************************************************************
**************************************************************************************************



gen crop_rot=.
la var crop_rot "Crop rotation"

bys time : su tree*
ta treebelt tree_any, m
egen tree = anymatch(tree_any), v(1)
* nota il punto interrogativo serve a non considerare le variabili il cui nome non ha un pezzo aggiuntivo quindi esclude una ipotetica variabile che si chiama solo tree
ta tree time, m
la var tree		"Agroforestry" 		/*Plot has tree to control soil erosion or HH had any trees*/
*************ANOMALY WARNING
replace tree=. if district==209 & time==0
*****************************************

gen cons_ag=1 if mintill==1 & (intleg_1==1 |  crop_rot==1) & (mulch==1 | covercrop_any==1)
recode cons_ag (.=0) if year==2013
la var cons_ag "Full set of CA "

gen msd_assoc=1 if mintill==1 & (intleg_1==1 |  crop_rot==1) 
recode msd_assoc (.=0) if year==2013
la var msd_assoc "MSD and association of crops "

gen msd_residues=1 if mintill==1 & (mulch==1 | covercrop_any==1) 
recode msd_residues (.=0) if year==2013
la var msd_residues "MSD and retained residues "


gen assoc_residues=1 if (intleg_1==1 |  crop_rot==1)  & (mulch==1 | covercrop_any==1) 
recode assoc_residues (.=0) if year==2013
la var assoc_residues "Association of crop  and retained residues "


gl y 	 mintill crop_rot intleg_1 covercrop_any mulch  msd_assoc msd_residues assoc_residues cons_ag ///
		tree  intercrop_other  swc_mech swc_bio swc_any fert_org_use 	///
		burn   
	
*bys time : su $y , sep(0) 	//	we have swc & fertilizer for 2010, not tillage & land prep stuff 
*svy : mean mintill mulch burn covercrop_any , over(time)	
*svy : mean swc_mech swc_bio swc_any fert_org_use intleg_1 intercrop_other con_ag tree , over(time)

*-> Consider combinations of practices 
	preserve
la def mintill 			0 "-mt" 1 "+mt"
la def swc_mech			0 "-sm" 1 "+sm"
la def swc_bio			0 "-sb" 1 "+sb"
la def swc_any			0 "-sa" 1 "+sa"
la def fert_org_use		0 "-fo" 1 "+fo"
la def mulch			0 "-mu" 1 "+mu"
la def burn				0 "-bu" 1 "+bu"
la def covercrop_any	0 "-cc" 1 "+cc"
la def intleg_1			0 "-l1" 1 "+l1"
la def intercrop_other 	0 "-ic" 1 "+ic"
la def crop_rot 		0 "-cr" 1 "+cr"
la def tree 			0 "-tr" 1 "+tr"
la def cons_ag 			0 "-ca" 1 "+ca"

foreach x of global y {
la val `x' `x' 
 }

	restore
		**	save temporary copy of plot-level data to be brought back at end of tests
tempfile restore
sa `restore'


********************************************************************************
********************************************************************************
********************************************************************************
**********************CREATE THE DATASET AT PLOT LEVEL *************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
qui{
drop if mi(keepforpanel)
destring case_id, gen(hhid)
format hhid %16.0g
assert !mi(hhid)
assert hhid==int(hhid)
*xtset hhid time	//	Need a plot version. 
drop hhid
bys time : distinct case_id
egen hhid = concat(case_id plotid)
replace hhid = subinstr(hhid,"R","0",1)
destring hhid, replace
replace hhid = . if mi(keepforpanel)
g time1 = time if !mi(keepforpanel)

destring case_id, gen(hhid1)
}
format hhid %16.0g
format hhid1 %16.0g
sa "$tmp_panel/MW_SLM_Panel_PLOT.dta", replace 
		//	end plot level panel dataset creation

	
/*	testing the unobserved heterogenity correlation with the error term (using hausman for FE vs RE)
	xtset hhid1
xtreg swc_mech $x_plot23, fe
est sto fe
xtreg swc_mech $x_plot23, re
est sto re
hausman fe re, sigmamore
reg swc_mech $x_plot23, vce(cluster psu)

*/

********************************************************************************
********************************************************************************
********************************************************************************
**********************CREATE THE DATASET AT HH LEVEL ***************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

qui {	
u "$tmp_panel/MW_SLM_Panel_PLOT.dta", clear
		preserve
keep if time==0
collapse (max) $y, by(case_id psu hh_wgt stratum keepforpanel time district $x_hh23 ///
	rain_rs_* tmax_00_rs_*)
su $y
tempfile ihs3
sa `ihs3'
		restore
isid case_id plotid time
		preserve
keep if time==1
collapse (max) $y, by(y2_hhid case_id psu hh_wgt stratum keepforpanel time district $x_hh23 ///
	rain_rs_* tmax_00_rs_* $x_sect23 )
tempfile ihps
sa `ihps'
		restore
	*	append ihs3 and ihps hh level datasets 
foreach d of global y {
loc lbl`d' : var lab `d'
								}
u `ihs3', clear
append using `ihps', gen(test)
assert test==time
drop test


	*	label hh level data 
foreach d of global y 		{
loc fix = "`lbl`d''"   /*loc fix = subinstr("`lbl`d''","Plot","HH had any plot that",1)*/
la var `d'	"`fix'"
								}


destring case_id, gen(hhid)
*replace hhid = . if mi(keepforpanel)
g time1 = time if !mi(keepforpanel)
sa "$tmp_panel/MW_SLM_Panel_HH.dta", replace 
	}	//	end HH level panel dataset creation. 
xtset hhid	//	this dataset is created to




********************************************************************************
********************************************************************************
********************************************************************************
**********************CREATE THE DATASET AT EA LEVEL ***************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
u "$tmp_panel/MW_SLM_Panel_PLOT.dta", clear

foreach d of varlist time $x_ea23 $y {
loc lbl`d' : var l `d'
								}

	*	get ihs3 and ihps ea level datasets 
		preserve
keep if time==0
collapse (first) time $x_ea23 rain_rs_?yr* tmax_00_rs_?yr* (mean) $y, by(ea_match)
tempfile ihs3
sa `ihs3'
		restore
		preserve
keep if time==1
collapse (first) time $x_ea23 rain_rs_?yr* tmax_00_rs_?yr* (mean) $y, by(ea_match)
tempfile ihps
sa `ihps'
		restore
	*	append ihs3 and ihps hh level datasets 
u `ihs3', clear
append using `ihps', gen(test)
assert test==time
drop test

	*	label ea level data 
foreach d of varlist time $x_ea23 $y {
la var `d' 	"`lbl`d''"
								}
foreach d of varlist $y 		{
loc lbl : var l `d'
loc fix = subinstr(subinstr(subinstr("`lbl'","Plot","EA mean of plots that",1),"was","were",1),"has","had",1)
la var `d'	"`fix'"
								}
sa "$tmp_panel/MW_SLM_Panel_EA.dta", replace 
	*}	//	end of ea level dataset creation 
	




********************************************************************************
********************************************************************************
********************************************************************************
**********************CREATE THE DATASET AT DISTRICT LEVEL *********************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
u "$tmp_panel/MW_SLM_Panel_HH.dta", clear

foreach d of varlist time $x_ea23 $y {
loc lbl`d' : var l `d'
								}

	*	get ihs3 and ihps ea level datasets 
		preserve
keep if time==0
collapse (first) time $x_ea23 rain_rs_?yr* tmax_00_rs_?yr* (mean) $y, by(district)
tempfile ihs3
sa `ihs3'
		restore
		preserve
keep if time==1
collapse (first) time $x_ea23 rain_rs_?yr* tmax_00_rs_?yr* (mean) $y, by(district)
tempfile ihps
sa `ihps'
		restore
	*	append ihs3 and ihps hh level datasets 
u `ihs3', clear
append using `ihps', gen(test)
assert test==time
drop test

	*	label district level data 
foreach d of varlist time $x_ea23 $y {
la var `d' 	"`lbl`d''"
								}
foreach d of varlist $y 		{
loc lbl : var l `d'
loc fix = subinstr(subinstr(subinstr("`lbl'","Plot","Districts mean of plots that",1),"was","were",1),"has","had",1)
la var `d'	"`fix'"
								}
sa "$tmp_panel/MW_SLM_Panel_DISTRICT.dta", replace 
	*}	//	end of ea level dataset creation 



************************************************************************************
*******************************************************************************
********************************************************************************
*/













*********************************************************************************
*****************ADOPTION DETERMINANTS******************************************
********************************************************************************

set more off
use "$out\final_dataset.dta", clear
qui : svyset psu [pw=panelweight], strata(strata)
est clear
bys hhid: gen nyear=[_N]
keep if nyear==2 

rename shcredit_MFIB_nbHHinEA shcredit_inEA
rename shext_Gov_nbHHinEA shext_inEA
rename shCFW_safnet_nbHHinEA shsafnet_inEA


rename nutrient_constrain_dummy1 no_nut_const
rename nutrient_constrain_dummy2 mod_nut_const
rename nutrient_constrain_dummy3 sev_nut_const
rename nutrient_constrain_dummy4 water

egen mod_sev_const= anymatch(mod_nut_const sev_nut_const), val(1)
lab var mod_sev_const "Moderate or Severe Nutrient Reterntion Constraint"
drop if water==1


replace maize_hybrid=1 if maize_hybrid>2 & maize_hybrid!=.







************************************USING ARC2 
gl practices 	mintill /*crop_rot*/ intleg_1 covercrop_any mulch  
		
		/*
		msd_assoc msd_residues assoc_residues cons_ag
		tree  intercrop_other  swc_mech swc_bio swc_any fert_org_use 	///
		burn   
		*/

gl independent covrain_lt  false_onset_prob mean_totrain_lt   /// /*  mean_tavg_lt*/
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 lnpganyudefl  shext_inEA shcredit_inEA  sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4

/*					
****************SIMPLE PROBIT MODEL	: tentative not using the command CMP: 			
				
				
foreach var of global practices {
svy: probit `var'  $independent if time==1
}




eststo mvprobit: mvprobit ( mintill		 : mintill	 =  $independent ) ///
		 ( intleg_1 	 				: intleg_1			=  $independent ) ///
		 ( covercrop_any : covercrop_any	=  $independent ) ///
		 ( mulch		 :  mulch			=  $independent ) ///
, beta0 seed(123456789) draws(3) cluster(psu)


 


preserve

*******balance panel
bys hhid: gen nyear=[_N]
keep if nyear==2
***************************
xtset hhid time


xtprobit intleg_1  $independent time, re vce(cluster psu) 
restore

*/

******************USING CMP COMMAND (MORE FLEXIBLE ALLOWS FOR SVY AND GIVE THE SAME RESULTS

****************************PROBIT MODEL


preserve
keep if time==1

eststo p_mintill:		cmp	( mintill 	 : mintill			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
 
 eststo p_intleg1:		 cmp	( intleg_1 	 : intleg_1			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_covercrop_any:  cmp	( covercrop_any 	 : covercrop_any			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_mulch:			 cmp	( mulch 	 : mulch		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest
 
 /*
 eststo p_tree:			svy: cmp	( tree 	 : tree		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 eststo p_swc_any:			svy: cmp	( swc_any 	 : swc_any		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 eststo p_fert_org_use:			svy: cmp	( fert_org_use 	 : fert_org_use		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
*/
 
 xml_tab  p_*,   stats(N F p)  t(Adoption determinants from Probit model)  c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Residue retention" /*"Agroforestry" "Erosion Control" "Organic Fertilizer"*/)   ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13)  font("Times New Roman" 10) save("$tables/adoption_arc2.xls") sh(PROBIT)  replace


*****************************************MVPROBIT MODEL

eststo mvprobit : cmp	( mintill		 	: mintill	        =  $independent ) ///
							( intleg_1 	 		: intleg_1			=  $independent ) ///
							( covercrop_any 	: covercrop_any		=  $independent ) ///
							( mulch		 		:  mulch			=  $independent ) /// 
 , ind($cmp_probit $cmp_probit $cmp_probit $cmp_probit) difficult technique(dfp)  ghkdraws(3, type(halton) anti) nolrtest
 xml_tab  mvprobit, stats(N F p) t(Adoption determinants from Multivariate Probit model) c(Constant) /*below*/ cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Residue retention" /*"Agroforestry" "Erosion Control" "Organic Fertilizer"*/) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_arc2.xls")   sh(MVPROBIT) append
 restore
 
 							/*
							( tree		 		:  tree				=  $independent ) ///
							( swc_any		 	:  swc_any			=  $independent ) ///
							( fert_org_use		:  fert_org_use		=  $independent ) ///
							*/

*********************************************PANEL PROBIT
preserve

xtset hhid year
 
eststo xtp_intleg1:   cmp	( intleg_1 	 : intleg_1			=  $independent time || hhid:)  ///
 , ind($cmp_probit)  cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 /*
 eststo xtp_tree:			 	cmp	( tree 	 : tree			= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 eststo xtp_swc_any:			cmp	( swc_any 	 : swc_any	= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 eststo xtp_fert_org_use:		cmp	( fert_org_use 	 : fert_org_use		= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 
*/ 
  xml_tab  xtp_*, stats(b N F p) t(Adoption determinants from Random-effects Probit models) c(Constant) /*below*/ cnames("Legume intercrop."  /*"Agroforestry" "Erosion Control" "Organic Fertilizer" */) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_arc2.xls") sh(XTPROBIT) append

restore






******************USING SPI


gl independent   neg_shock_prob pos_shock_prob covrain_lt  false_onset_prob  mean_totrain_lt  ///
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 lnpganyudefl  shext_inEA shcredit_inEA sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4



			
****************************PROBIT MODEL

preserve
keep if time==1

eststo p_mintill:		 cmp	( mintill 	 	: mintill			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
  eststo p_intleg1:		 cmp	( intleg_1 	 	: intleg_1			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
eststo p_covercrop_any: cmp	( covercrop_any : covercrop_any		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_mulch:			 cmp	( mulch 	 	: mulch				= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 xml_tab  p_*,   stats(N F p)  t(Adoption determinants from Probit model)  c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching")   ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13)  font("Times New Roman" 10) save("$tables/adoption_spi.xls") sh(PROBIT)  replace

*****************************************MVPROBIT MODEL 
eststo mvprobit : cmp	( mintill		 : mintill	        =  $independent ) ///
							( intleg_1 	 	 : intleg_1			=  $independent ) ///
							( covercrop_any  : covercrop_any	=  $independent ) ///
							( mulch		 	 :  mulch			=  $independent ) ///
 , ind($cmp_probit $cmp_probit $cmp_probit $cmp_probit) difficult technique(dfp)  ghkdraws(3, type(halton) anti) nolrtest
 xml_tab  mvprobit, stats(N F p) t(Adoption determinants from Multivariate Probit model) c(Constant) /*below*/ cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching") drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_spi.xls")   sh(MVPROBIT) append
 restore
 
 
 

*******************************PANEL PROBIT
preserve

xtset hhid year
  
eststo xtp_intleg1:   cmp	( intleg_1 	 : intleg_1			=  $independent time || hhid:)  ///
 , ind($cmp_probit)  cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 
  xml_tab  xtp_intleg1, stats(b N F p) t(Adoption determinants from Random-effects Probit models) c(Constant) /*below*/ cnames("Legume intercrop." ) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_spi.xls") sh(XTPROBIT) append

restore








********************************************REPEAT THE ANALYSIS WITH SAPP DISTRICT
keep if district==101 | district==103 | district==104 | district==202 | district==206 | district==208 | district==209 | district==303 | district==304 | district==305 | district==312
tempfile sapp_district
save `sapp_district'		


qui : svyset psu [pw=panelweight], strata(strata)


************************************ USING ARC2 
gl practices 	mintill /*crop_rot*/ intleg_1 covercrop_any mulch  
		
		/*
		msd_assoc msd_residues assoc_residues cons_ag
		tree  intercrop_other  swc_mech swc_bio swc_any fert_org_use 	///
		burn   
		*/

gl independent  covrain_lt false_onset_prob  mean_totrain_lt   /// /*mean_tavg_lt*/
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 lnpganyudefl  shext_inEA shcredit_inEA  sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4


				
/*					
****************SIMPLE PROBIT MODEL	: tentative not using the command CMP: 		
				
				
foreach var of global practices {
svy: probit `var'  $independent if time==1
}




eststo mvprobit: mvprobit ( mintill		 : mintill	 =  $independent ) ///
		 ( intleg_1 	 : intleg_1			=  $independent ) ///
		 ( covercrop_any : covercrop_any	=  $independent ) ///
		 ( mulch		 :  mulch			=  $independent ) ///
, beta0 seed(123456789) draws(3) cluster(psu)


 


preserve

*******balance panel
bys hhid: gen nyear=[_N]
keep if nyear==2
***************************
xtset hhid time


xtprobit intleg_1  $independent time, re vce(cluster psu) 
restore

*/




******************USING CMP COMMAND (MORE FLEXIBLE ALLOWS FOR SVY AND GIVE THE SAME RESULTS

****************************PROBIT MODEL


preserve
keep if time==1

eststo p_mintill:		 cmp	( mintill 	 : mintill			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
 
 eststo p_intleg1:		 cmp	( intleg_1 	 : intleg_1			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_covercrop_any:  cmp	( covercrop_any 	 : covercrop_any			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_mulch:			 cmp	( mulch 	 : mulch		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest
 
 /*
 eststo p_tree:			svy: cmp	( tree 	 : tree		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 eststo p_swc_any:			svy: cmp	( swc_any 	 : swc_any		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 eststo p_fert_org_use:			svy: cmp	( fert_org_use 	 : fert_org_use		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
*/
 
 xml_tab  p_*,   stats(N F p)  t(Adoption determinants from Probit model)  c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching" /*"Agroforestry" "Erosion Control" "Organic Fertilizer"*/)   ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13)  font("Times New Roman" 10) save("$tables/adoption_arc2_sapp.xls") sh(PROBIT)  replace


*****************************************MVPROBIT MODEL

eststo mvprobit : cmp	( mintill		 	: mintill	        =  $independent ) ///
							( intleg_1 	 		: intleg_1			=  $independent ) ///
							( covercrop_any 	: covercrop_any		=  $independent ) ///
							( mulch		 		:  mulch			=  $independent ) /// 
 , ind($cmp_probit $cmp_probit $cmp_probit $cmp_probit) difficult technique(dfp)  ghkdraws(3, type(halton) anti) nolrtest
 xml_tab  mvprobit, stats(N F p) t(Adoption determinants from Multivariate Probit model) c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching" /*"Agroforestry" "Erosion Control" "Organic Fertilizer"*/) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_arc2_sapp.xls")   sh(MVPROBIT) append
 restore
 
 							/*
							( tree		 		:  tree				=  $independent ) ///
							( swc_any		 	:  swc_any			=  $independent ) ///
							( fert_org_use		:  fert_org_use		=  $independent ) ///
							*/

*********************************************PANEL PROBIT
preserve

xtset hhid year
 
eststo xtp_intleg1:   cmp	( intleg_1 	 : intleg_1			=  $independent time || hhid:)  ///
 , ind($cmp_probit)  cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 /*
 eststo xtp_tree:			 	cmp	( tree 	 : tree			= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 eststo xtp_swc_any:			cmp	( swc_any 	 : swc_any	= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 

 eststo xtp_fert_org_use:		cmp	( fert_org_use 	 : fert_org_use		= $independent time || hhid:)  ///
 , ind($cmp_probit) cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 
*/ 
  xml_tab  xtp_*, stats(b N F p) t(Adoption determinants from Random-effects Probit models) c(Constant) below cnames("Legume intercrop."  /*"Agroforestry" "Erosion Control" "Organic Fertilizer" */) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_arc2_sapp.xls") sh(XTPROBIT) append

restore






******************USING SPI


gl independent   neg_shock_prob pos_shock_prob covrain_lt  false_onset_prob  mean_totrain_lt   ///
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 lnpganyudefl  shext_inEA shcredit_inEA  sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4




			
****************************PROBIT MODEL

preserve
keep if time==1

eststo p_mintill:		 cmp	( mintill 	 	: mintill			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
  eststo p_intleg1:		 cmp	( intleg_1 	 	: intleg_1			= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 
 
eststo p_covercrop_any:  cmp	( covercrop_any : covercrop_any		= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

eststo p_mulch:			 cmp	( mulch 	 	: mulch				= $independent )  ///
 , ind($cmp_probit)   ghkdraws(3, type(halton) anti) nolrtest 

 xml_tab  p_*,   stats(N F p)  t(Adoption determinants from Probit model)  c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching")   ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13)  font("Times New Roman" 10) save("$tables/adoption_spi_sapp.xls") sh(PROBIT)  replace

*****************************************MVPROBIT MODEL 
eststo mvprobit : cmp	( mintill		 : mintill	        =  $independent ) ///
							( intleg_1 	 	 : intleg_1			=  $independent ) ///
							( covercrop_any  : covercrop_any	=  $independent ) ///
							( mulch		 	 :  mulch			=  $independent ) ///
 , ind($cmp_probit $cmp_probit $cmp_probit $cmp_probit) difficult technique(dfp)  ghkdraws(3, type(halton) anti) nolrtest
 xml_tab  mvprobit, stats(N F p) t(Adoption determinants from Multivariate Probit model) c(Constant) below cnames("Minimum Soil Disturbance" "Legume intercrop." "Cover crop"  "Mulching") drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_spi_sapp.xls")   sh(MVPROBIT) append
 restore
 
 
 

*******************************PANEL PROBIT
preserve
xtset hhid year
  
eststo xtp_intleg1:   cmp	( intleg_1 	 : intleg_1			=  $independent time || hhid:)  ///
 , ind($cmp_probit)  cluster(psu)  ghkdraws(3, type(halton) anti) nolrtest 
  xml_tab  xtp_intleg1, stats(b N F p) t(Adoption determinants from Random-effects Probit models) c(Constant) below cnames("Legume intercrop." ) drop() ///
lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) save("$tables/adoption_spi_sapp.xls") sh(XTPROBIT) append

restore


	
	
	
	
	
	
	
			
					
					
					
					
					
					
					
					
					
		
				
					
					
					
*********************************************************************************
***************************IMPACT***********************************************
********************************************************************************
use "$out\final_dataset.dta", clear
bys hhid: gen nyear=[_N]
keep if nyear==2 

capture program drop mydisp
	program mydisp, eclass
						matrix betas=e(b)
						matrix colnames betas =   "intleg_1" ///
					"rain_season" "tmax28"  ///
					"no_nut_const"  			///
					"hhsize"  "agehead" "femhead"  "educhigh"   ///
					"landown" "tlu_oxen_cattle"  /// /*wealth_ag*/
					"irrigation_scheme"    ///
					 "log_fertpEA"  "log_ganyupEA" "log_maizepEA" "shext_inEA" "shcredit_inEA"  "sh_fisp_EA" "shsafnet_inEA"   ///								
					"agroeco_dummy1" "agroeco_dummy3" "agroeco_dummy4" ///
					"dYEAR" ///
					"mean__rain_season" "mean__tmax28" ///
					"mean__hhsize" "mean__femhead " "mean__educhigh"  ///
					"mean__landown" "mean__tlu" ///
					"mean__irrigation" ///
					"mean__log_fertpEA" "mean__log_ganyupEA" "mean__log_maizepEA"  "mean__shext_inEA" "mean__shcredit_inEA" "mean__sh_fisp_EA" "mean__shsafnet_inEA" ///
					
					
					mat list betas
					ereturn repost b = betas, rename
					end
					





global Yield "yield_maize value_total_yield"
global Welfare " netincome consumption_pc poverty  "
global Food_security "food_pc caloric_intake"

foreach y of global Yield {
if "`y'"=="yield_maize" {
set more off
use "$out\final_dataset_plot.dta", clear
qui : svyset psu [pw=panelweight], strata(strata)
foreach v of varlist yield_maize srtm {
replace `v'= log(`v')
}

rename shcredit_MFIB_nbHHinEA shcredit_inEA
rename shext_Gov_nbHHinEA shext_inEA
rename shCFW_safnet_nbHHinEA shsafnet_inEA



rename nutrient_constrain_dummy1 no_nut_const
rename nutrient_constrain_dummy2 mod_nut_const
rename nutrient_constrain_dummy3 sev_nut_const
rename nutrient_constrain_dummy4 water



egen mod_sev_const= anymatch(mod_nut_const sev_nut_const), v(1)
la var mod_sev_const "Moderate or Severe Nutrient Capacity Constraints"

drop if water==1


bys ea_id: gen tmax28=mean_tmax_season>28
lab var tmax28 "Max temp 28°C (%)" 
rename  mean_rain_season  rain_season 
rename fisp_salespc_kg fisp_pc

gl practices 	mintill  intleg_1 covercrop_any mulch  /*msd_assoc msd_residues assoc_residues*/ 

///tree  intercrop_other  swc_mech swc_bio swc_any fert_org_use 	
		/*burn*/   

gl independent 	 rain_season tmax28  ///
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 log_fertpEA  log_ganyupEA log_maizepEA  shext_inEA shcredit_inEA  sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4


********************************************************************************
********************************OLS****************************************
********************************************************************************

est clear
 reg yield_maize $practices $independent, vce(cluster psu)
est store ols_1
foreach x of global practices {
preserve
reg yield_maize `x' $independent, vce(cluster psu) 
est store `x'
restore
}
noi xml_tab * , stats(N r2)  c(Constant) t("Impact of the adoption of CSA on `y''") lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) keep($practices  $independent   )  save("$tables/impact_arc2.xls") sheet(yield_maize_ols)  note("Note:") ///
  star(* 0.1 ** 0.05 *** 0.01)  /* below*/ long  replace

  
********************************PANEL REGRESSION********************************
gl practices 	 intleg_1 

preserve
egen panel_id= group(hhid plot_id)

xtset panel_id year
tempfile panel_balanced
save `panel_balanced'
restore

use `panel_balanced', clear


***************************************** POOLED OLS
 reg yield_maize $practices $independent dYEAR, vce(cluster psu)
est store ols

************************RANDOM EFFECT

xtreg yield_maize $practices $independent dYEAR, re vce(cluster psu)  sa
est store re

/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , re vce(cluster psu) 
est store `x'
restore
}
*/

  
 ************************FIXED EFFECT

xtreg yield_maize $practices $independent dYEAR, fe vce(cluster psu) 
est store fe
/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , fe vce(cluster psu) 
est store `x'
restore
}
*/

 
************************CORRELATED EFFECT
/*
foreach var of global independent {
bys panel_id: gen perc_diff_`var'=(`var'- `var'[_n-1])/`var'[_n-1] *100
recode perc_diff_`var' (.=0)
bys panel_id: replace  perc_diff_`var'= perc_diff_`var'[_n+1] if year==2010
egen average_diff`var'= mean(perc_diff_`var') 
if average_diff`var'>10 | average_diff`var'<-10 {
bys ea_id: egen cre_mean_`var'= mean(`var') 
}
}

rename cre_mean_irrigation_scheme cre_irrig_scheme
rename cre_mean_agriculture_ca cre_ag_ca
rename cre_mean_shcredit_inEA cre_shcredit
rename cre_mean_shext_inEA cre_shext
rename cre_mean_shsafnet_inEA cre_shsafnet

gl independent 	 rain_season tmax28  ///
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				 shext_inEA shcredit_inEA  sh_fisp_EA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4
				 cre*
*/

			
					
mundlak yield_maize $practices $independent dYEAR, percentage(10) nocomparison
mydisp 
est store mundlak
/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , re vce(cluster psu) 
est store `x'
restore
}
*/
noi xml_tab ols re fe mundlak , stats(N r2_w r2_b r2_o r2 )  c(Constant) t("Impact of the adoption of CSA on `y''") lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) keep(intleg_1   $independent dYEAR  mean* )  save("$tables/impact_arc2.xls") sheet(`y')  note("Note:") ///
  star(* 0.1 ** 0.05 *** 0.01)   /*below*/ long  append
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  
  else if "`y'"=="value_total_yield" { 
  set more off
use "$out\final_dataset.dta", clear
qui : svyset psu [pw=panelweight], strata(strata)
bys hhid: gen nyear=[_N]
keep if nyear==2 


foreach v of varlist value_total_yield srtm {
replace `v'= log(`v')
}

rename shcredit_MFIB_nbHHinEA shcredit_inEA
rename shext_Gov_nbHHinEA shext_inEA
rename shCFW_safnet_nbHHinEA shsafnet_inEA


rename nutrient_constrain_dummy1 no_nut_const
rename nutrient_constrain_dummy2 mod_nut_const
rename nutrient_constrain_dummy3 sev_nut_const
rename nutrient_constrain_dummy4 water

egen mod_sev_const= anymatch(mod_nut_const sev_nut_const), v(1)
la var mod_sev_const "Moderate or Severe Nutrient Capacity Constraints"
drop if water==1

drop if water==1

bys ea_id: gen tmax28=mean_tmax_season>28
rename  mean_rain_season  rain_season 
rename fisp_salespc_kg fisp_pc

gl practices 	mintill  intleg_1 covercrop_any mulch  /*msd_assoc msd_residues assoc_residues*/  

///tree  intercrop_other  swc_mech swc_bio swc_any fert_org_use 	
		/*burn*/   

gl independent 	 rain_season  tmax28  ///
				 no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  tlu_oxen_cattle  /// /*wealth_ag */
				 irrigation_scheme    ///
				  log_fertpEA  log_ganyupEA log_maizepEA   shext_inEA shcredit_inEA  sh_fisp_EA shsafnet_inEA  ///
				 agroeco_dummy1 agroeco_dummy3 agroeco_dummy4

********************************************************************************
********************************OLS****************************************
********************************************************************************


est clear
reg value_total_yield $practices $independent, vce(cluster psu) 
est store ols_1
foreach x of global practices {
preserve
reg value_total_yield `x' $independent, vce(cluster psu) 
est store `x'
restore
}
  noi xml_tab * , stats(N r2)  c(Constant) t("Impact of the adoption of CSA on `y''") lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) keep($practices  $independent   )  save("$tables/impact_arc2.xls") sheet(value_total_yield_ols)  note("Note:") ///
  star(* 0.1 ** 0.05 *** 0.01)   /*below*/ long  append
********************************PANEL REGRESSION********************************
gl practices 	 intleg_1 

preserve

xtset hhid year
tempfile panel_balanced
save `panel_balanced'
restore

use `panel_balanced', clear

********************************POOLED OLS
*****************************************OLS
 reg value_total_yield $practices $independent dYEAR, vce(cluster psu)
est store ols
************************RANDOM EFFECT

xtreg value_total_yield $practices $independent dYEAR, re vce(cluster psu) 
est store re
/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , re vce(cluster psu) 
est store `x'
restore
}
*/

  
 ************************FIXED EFFECT

xtreg value_total_yield $practices $independent dYEAR, fe vce(cluster psu) 
est store fe
/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , fe vce(cluster psu) 
est store `x'
restore
}
*/


 
************************CORRELATED EFFECT
/*
foreach var of global independent {
bys panel_id: gen perc_diff_`var'=(`var'- `var'[_n-1])/`var'[_n-1] *100
recode perc_diff_`var' (.=0)
bys panel_id: replace  perc_diff_`var'= perc_diff_`var'[_n+1] if year==2010
egen average_diff`var'= mean(perc_diff_`var') 
if average_diff`var'>10 | average_diff`var'<-10 {
bys ea_id: egen cre_mean_`var'= mean(`var') 
}
}

rename cre_mean_irrigation_scheme cre_irrig_scheme
rename cre_mean_agriculture_ca cre_ag_ca
rename cre_mean_shcredit_inEA cre_shcredit
rename cre_mean_shext_inEA cre_shext
rename cre_mean_shsafnet_inEA cre_shsafnet

gl independent 	 srtm  no_nut_const  			///
				 hhsize  agehead femhead  educhigh    ///
				 landown  wealth_ag tlu_oxen_cattle  ///
				 fert_org_use fert_inorg_use pest_use ///
				 irrigation_scheme extension  agriculture_ca ///
				 log_ganyuEA pricemaizeEArepl pricefertEArepl shext_inEA shcredit_inEA shsafnet_inEA  ///
				 agroeco_dummy2 agroeco_dummy3 agroeco_dummy4 ///
				 cre*
*/


					
mundlak value_total_yield $practices $independent dYEAR, percentage(10) nocomparison 
mydisp		
est store mundlak
/*
foreach x of global practices {
preserve
xtreg yield_maize `x' $independent , re vce(cluster psu) 
est store `x'
restore
}
*/

noi xml_tab ols re fe mundlak , stats(N r2_w r2_b r2_o r2 )   c(Constant) t("Impact of the adoption of CSA on `y''") lines(SCOL_NAMES 13 COL_NAMES 2 _cons 2 LAST_ROW 13) font("Times New Roman" 10) keep(intleg_1   $independent dYEAR  mean*)  save("$tables/impact_arc2.xls") sheet(`y')  note("Note:") ///
  star(* 0.1 ** 0.05 *** 0.01)   /*below*/ long  append
  }
  }
  
  
  
 
  
  